var jobId = T.p("jobId");
$(function () {
    $("#jqGrid").jqGrid({
        url: '../scheduleJobDtl/list',
        datatype: "json",
        postData:{jobId:jobId},
        colModel: [
			{ label: 'id', name: 'id', width: 50, key: true },
			{ label: '任务ID', name: 'jobId',index:'job_id', width: 80 },
			{ label: '明细类型 workSubject:作业专题 workItem：作业项', name: 'type',index:'type', width: 80 },
			{ label: '作业专题id', name: 'workSubjectId',index:'work_subject_id', width: 80 },
			// { label: '作业专题名称', name: 'workSubjectName',index:'work_subject_name', width: 80 },
			{ label: '作业项id', name: 'workId',index:'work_id', width: 80 },
			// { label: '作业项名称', name: 'workItemName',index:'work_item_name', width: 80 },
			{ label: '执行顺序', name: 'execNum',index:'exec_num', width: 80,  formatter: function(val, obj, row, act){
                    var html = [];
                    html.push('<input type="hidden" name="ids"  value="'+row.id+'"/>');
                    html.push('<input name="sorts"  type="number" value="'+row.execNum+'" style="width:50px;height:19px;margin:0;padding:0;text-align:center;border:1px solid #ddd">');
                    return html.join('');
			}},
			{ label: 'createTime', name: 'createTime',index:'create_time', width: 80 },
			{ label: 'updateTime', name: 'updateTime',index:'update_time', width: 80 }
        ],
		viewrecords: true,
        height: 800,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
			root: "page.data",
			page: "page.pageNo",
			total: "page.pageCount",
			records: "page.count"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		isShowCond:false,
        btnText:'查询',
        editSort:{
            ids :[],
            sorts :[]
        },
        qryForm:{
        }
	},
	methods: {
        updateSort:function(){
            var ids =[];
            $("input[name='ids']").each(function () {
                ids.push($(this).val());
            })
            var sorts = [];
            $("input[name='sorts']").each(function () {
                sorts.push($(this).val());
            })
            this.editSort.ids = ids;
            this.editSort.sorts = sorts;
            $.ajax({
                type: "POST",
                url: "../scheduleJobDtl/updateSort",
                data: this.editSort,
                contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                traditional: true,
                dataType: "json",
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },
		reset: function(){

        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{
                },
                page:1
            }).trigger("reloadGrid");
        },
        toggle:function(){
            this.isShowCond = !this.isShowCond;

            if(this.isShowCond==true){
                this.btnText = "隐藏"
            }else if(this.isShowCond==false){
                this.btnText = "查询"
            }

        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			
			location.href = "scheduleJobDtl_add.html?id="+id;
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../scheduleJobDtl/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},back:function (event) {
            history.go(-1);
        },addWorkSubject:function () {
            layer.open({
                type: 2,
                offset: '50px',
                skin: 'layui-layer-lan',
                title: "选择专题",
                area: ['800px', '650px'],
                shade: 0,
                shadeClose: false,
                maxmin: true,
                content: '../../work/workSubject.html?layerFlag=true&jobId='+jobId,
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                    var w = $(layero).find("iframe")[0].contentWindow;//通过该对象可以获取iframe中的变量，调用iframe中的方法
					var selectedRows = w.getSelectedRows(false);
                    $.ajax({
                        type: "POST",
                        url: "../scheduleJobDtl/saveJobDtl",
                        data: {ids:selectedRows,type:'workSubject',jobId:jobId},
                        contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        traditional: true,
                        dataType: "json",
                        success: function(r){
                            if(r.code == 0){
                                alert('操作成功', function(index){
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });

                    layer.close(index);
                }
            });
        },addWorkItem:function () {
            layer.open({
                type: 2,
                offset: '50px',
                skin: 'layui-layer-lan',
                title: "选择专题",
                area: ['900px', '650px'],
                shade: 0,
                shadeClose: false,
                maxmin: true,
                content: '../../work/work.html?layerFlag=true&jobId='+jobId,
                btn: ['确定', '取消'],
                btn1: function (index,layero) {
                    var w = $(layero).find("iframe")[0].contentWindow;//通过该对象可以获取iframe中的变量，调用iframe中的方法
                    var selectedRows = w.getSelectedRows(false);
                    $.ajax({
                        type: "POST",
                        url: "../scheduleJobDtl/saveJobDtl",
                        data: {ids:selectedRows,type:'workItem',jobId:jobId},
                        contentType:'application/x-www-form-urlencoded; charset=UTF-8',
                        traditional: true,
                        dataType: "json",
                        success: function(r){
                            if(r.code == 0){
                                alert('操作成功', function(index){
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });

                    layer.close(index);
                }
            });
        }
	}
});
